package com.topView2;

/**
 * @Author: huangzhigao
 * @Date: 2023/2/11 16:50
 */
public class leecode8 {
    public int myAtoi(String s) {
        int i = 0;
        int n = s.length();
        int sigh = 1;
        long res = 0;
        //跳过空格
        while (i < n && s.charAt(i) == ' ') {
            i++;
        }
        if (i == n) {
            return 0;
        }

        if (s.charAt(i) == '-') {
            sigh = -1;
            i++;
        } else if (s.charAt(i) == '+') {
            sigh = 1;
            i++;
        }
        if (i == n) {
            return 0;
        }
        while (i < n && s.charAt(i) >= '0' && s.charAt(i) <= '9') {
            res = res * 10 + (s.charAt(i) - '0');
            if (res > Integer.MAX_VALUE) {
                break;
            }
            i++;
        }

        if ((int) res != res) {
            return sigh == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
        }
        return (int) res * sigh;


    }
}
